home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / Win32CEGuiRendererSelector.cpp < prev   
C/C++ Source or Header  |  2005-11-15  |  16KB  |  453 lines

  1. /************************************************************************
  2.     filename:   Win32CEGuiRendererSelector.cpp
  3.     created:    24/9/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #include "Win32CEGuiRendererSelector.h"
  25. #include <TCHAR.H>
  26.  
  27.  
  28. /*************************************************************************
  29.     Constructor
  30. *************************************************************************/
  31. Win32CEGuiRendererSelector::Win32CEGuiRendererSelector() :
  32.         d_template(createDialogTemplate())
  33. {}
  34.  
  35.  
  36. /*************************************************************************
  37.     Destructor
  38. *************************************************************************/
  39. Win32CEGuiRendererSelector::~Win32CEGuiRendererSelector()
  40. {
  41.     if (d_template)
  42.     {
  43.         LocalFree(d_template);
  44.     }
  45. }
  46.  
  47.  
  48. /*************************************************************************
  49.     Display the dialog and wait for user
  50. *************************************************************************/
  51. bool Win32CEGuiRendererSelector::inkokeDialog()
  52. {
  53.     if (d_template)
  54.     {
  55.         // show dialog & return result
  56.         return (1 == DialogBoxIndirectParam(GetModuleHandle(NULL), d_template, NULL, Win32CEGuiRendererSelector::dialogProcedure, reinterpret_cast<LPARAM>(this)));
  57.     }
  58.  
  59.     return false;
  60. }
  61.  
  62.  
  63. /*************************************************************************
  64.     Create Win32 dialog template
  65. *************************************************************************/
  66. LPDLGTEMPLATE Win32CEGuiRendererSelector::createDialogTemplate()
  67. {
  68.     SIZE_T  templateBufferSize = 1024;
  69.  
  70.     // allocate memory to hold the template we're going to construct
  71.     LPDLGTEMPLATE dialogTemplate = static_cast<LPDLGTEMPLATE>(LocalAlloc(LPTR, templateBufferSize));
  72.  
  73.     // if allocation was successful
  74.     if (dialogTemplate)
  75.     {
  76.         LPDLGITEMTEMPLATE item;
  77.         LPBYTE buffer = reinterpret_cast<LPBYTE>(dialogTemplate);
  78.  
  79.         //
  80.         // build template header
  81.         //
  82.         LPDLGTEMPLATE header    = reinterpret_cast<LPDLGTEMPLATE>(buffer);
  83.         header->style           = DS_MODALFRAME|WS_CAPTION|WS_VISIBLE;
  84.         header->dwExtendedStyle = 0;
  85.         header->cdit            = 6;
  86.         header->x               = (short)0x8000;
  87.         header->y               = (short)0x8000;
  88.         header->cx              = 150;
  89.         header->cy              = 75;
  90.  
  91.         // advance buffer pointer
  92.         buffer += sizeof(DLGTEMPLATE);
  93.  
  94.         //
  95.         // Write null menu and class names
  96.         //
  97.         *reinterpret_cast<LPWORD>(buffer) = 0;
  98.         buffer += sizeof(WORD);
  99.  
  100.         *reinterpret_cast<LPWORD>(buffer) = 0;
  101.         buffer += sizeof(WORD);
  102.  
  103.         //
  104.         // Write dialog title
  105.         //
  106.         int charCount = copyAnsiToWideChar(buffer, TEXT("CEGui - Renderer Selection"));
  107.         buffer += charCount * sizeof(WORD);
  108.  
  109.         // align pointer for first item
  110.         buffer = alignPointer(buffer);
  111.  
  112.         //
  113.         // Buttons area static frame
  114.         //
  115.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  116.         item->style             = BS_GROUPBOX|WS_VISIBLE|WS_CHILD;
  117.         item->dwExtendedStyle   = 0;
  118.         item->x                 = 5;
  119.         item->y                 = 48;
  120.         item->cx                = 140;
  121.         item->cy                = 22;
  122.         item->id                = 0;
  123.  
  124.         // advance buffer pointer
  125.         buffer += sizeof(DLGITEMTEMPLATE);
  126.  
  127.         // write class information
  128.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  129.         buffer += sizeof(WORD);
  130.  
  131.         *reinterpret_cast<LPWORD>(buffer) = 0x0080;
  132.         buffer += sizeof(WORD);
  133.  
  134.         // write caption
  135.         charCount = copyAnsiToWideChar(buffer, TEXT(""));
  136.         buffer += charCount * sizeof(WORD);
  137.  
  138.         // no creation data
  139.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  140.         buffer += sizeof(WORD);
  141.  
  142.         // align pointer for next item
  143.         buffer = alignPointer(buffer);
  144.  
  145.         //
  146.         // Selection area static frame
  147.         //
  148.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  149.         item->style             = BS_GROUPBOX|WS_VISIBLE|WS_CHILD;
  150.         item->dwExtendedStyle   = 0;
  151.         item->x                 = 5;
  152.         item->y                 = 0;
  153.         item->cx                = 140;
  154.         item->cy                = 50;
  155.         item->id                = 0;
  156.  
  157.         // advance buffer pointer
  158.         buffer += sizeof(DLGITEMTEMPLATE);
  159.  
  160.         // write class information
  161.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  162.         buffer += sizeof(WORD);
  163.  
  164.         *reinterpret_cast<LPWORD>(buffer) = 0x0080;
  165.         buffer += sizeof(WORD);
  166.  
  167.         // write caption
  168.         charCount = copyAnsiToWideChar(buffer, TEXT(""));
  169.         buffer += charCount * sizeof(WORD);
  170.  
  171.         // no creation data
  172.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  173.         buffer += sizeof(WORD);
  174.  
  175.         // align pointer for next item
  176.         buffer = alignPointer(buffer);
  177.  
  178.         //
  179.         // Okay button
  180.         //
  181.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  182.         item->style             = BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD|WS_TABSTOP;
  183.         item->dwExtendedStyle   = 0;
  184.         item->x                 = 9;
  185.         item->y                 = 55;
  186.         item->cx                = 40;
  187.         item->cy                = 12;
  188.         item->id                = IDOK;
  189.  
  190.         // advance buffer pointer
  191.         buffer += sizeof(DLGITEMTEMPLATE);
  192.  
  193.         // write class information
  194.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  195.         buffer += sizeof(WORD);
  196.  
  197.         *reinterpret_cast<LPWORD>(buffer) = 0x0080;
  198.         buffer += sizeof(WORD);
  199.  
  200.         // write caption
  201.         charCount = copyAnsiToWideChar(buffer, TEXT("Go!"));
  202.         buffer += charCount * sizeof(WORD);
  203.  
  204.         // no creation data
  205.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  206.         buffer += sizeof(WORD);
  207.  
  208.         // align pointer for next item
  209.         buffer = alignPointer(buffer);
  210.  
  211.         //
  212.         // Cancel button
  213.         //
  214.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  215.         item->style             = BS_PUSHBUTTON|WS_VISIBLE|WS_CHILD|WS_TABSTOP;
  216.         item->dwExtendedStyle   = 0;
  217.         item->x                 = 101;
  218.         item->y                 = 55;
  219.         item->cx                = 40;
  220.         item->cy                = 12;
  221.         item->id                = IDCANCEL;
  222.  
  223.         // advance buffer pointer
  224.         buffer += sizeof(DLGITEMTEMPLATE);
  225.  
  226.         // write class information
  227.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  228.         buffer += sizeof(WORD);
  229.  
  230.         *reinterpret_cast<LPWORD>(buffer) = 0x0080;
  231.         buffer += sizeof(WORD);
  232.  
  233.         // write caption
  234.         charCount = copyAnsiToWideChar(buffer, TEXT("Cancel"));
  235.         buffer += charCount * sizeof(WORD);
  236.  
  237.         // no creation data
  238.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  239.         buffer += sizeof(WORD);
  240.  
  241.         // align pointer for next item
  242.         buffer = alignPointer(buffer);
  243.  
  244.         //
  245.         // Combo label
  246.         //
  247.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  248.         item->style             = SS_LEFT|WS_VISIBLE|WS_CHILD|WS_GROUP;
  249.         item->dwExtendedStyle   = 0;
  250.         item->x                 = 8;
  251.         item->y                 = 7;
  252.         item->cx                = 130;
  253.         item->cy                = 12;
  254.         item->id                = 0;
  255.  
  256.         // advance buffer pointer
  257.         buffer += sizeof(DLGITEMTEMPLATE);
  258.  
  259.         // write class information
  260.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  261.         buffer += sizeof(WORD);
  262.  
  263.         *reinterpret_cast<LPWORD>(buffer) = 0x0082;
  264.         buffer += sizeof(WORD);
  265.  
  266.         // write caption
  267.         charCount = copyAnsiToWideChar(buffer, TEXT("Select Renderer to Use:"));
  268.         buffer += charCount * sizeof(WORD);
  269.  
  270.         // no creation data
  271.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  272.         buffer += sizeof(WORD);
  273.  
  274.         // align pointer for next item
  275.         buffer = alignPointer(buffer);
  276.  
  277.         //
  278.         // Combobox
  279.         //
  280.         item = reinterpret_cast<LPDLGITEMTEMPLATE>(buffer);
  281.         item->style             = CBS_DROPDOWNLIST|WS_VISIBLE|WS_CHILD;
  282.         item->dwExtendedStyle   = 0;
  283.         item->x                 = 8;
  284.         item->y                 = 19;
  285.         item->cx                = 130;
  286.         item->cy                = 100;
  287.         item->id                = 1000;
  288.  
  289.         // advance buffer pointer
  290.         buffer += sizeof(DLGITEMTEMPLATE);
  291.  
  292.         // write class information
  293.         *reinterpret_cast<LPWORD>(buffer) = 0xFFFF;
  294.         buffer += sizeof(WORD);
  295.  
  296.         *reinterpret_cast<LPWORD>(buffer) = 0x0085;
  297.         buffer += sizeof(WORD);
  298.  
  299.         // write caption
  300.         charCount = copyAnsiToWideChar(buffer, TEXT(""));
  301.         buffer += charCount * sizeof(WORD);
  302.  
  303.         // no creation data
  304.         *reinterpret_cast<LPWORD>(buffer) = 0x0000;
  305.         buffer += sizeof(WORD);
  306.  
  307.         // align pointer for next item
  308.         buffer = alignPointer(buffer);
  309.  
  310.         return dialogTemplate;
  311.     }
  312.  
  313.     return 0;
  314. }
  315.  
  316.  
  317. /*************************************************************************
  318.     Win32 dialog procedure function
  319. *************************************************************************/
  320. INT_PTR CALLBACK Win32CEGuiRendererSelector::dialogProcedure(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  321. {
  322.     switch(message)
  323.     {
  324.     case WM_INITDIALOG:
  325.         {
  326.             // get the 'this' ptr we were passed
  327.             Win32CEGuiRendererSelector* obj = reinterpret_cast<Win32CEGuiRendererSelector*>(lParam);
  328.  
  329.             // set as window long for future use
  330.             SetWindowLong(hDlg, DWL_USER, static_cast<LONG>(lParam));
  331.  
  332.             //
  333.             // Set-up combo box list
  334.             //
  335.             // get combo control
  336.             HWND combo = GetDlgItem(hDlg, 1000);
  337.  
  338.             if (combo)
  339.             {
  340.                 // basic control setup
  341.                 int idx;
  342.  
  343.                 // clear old data
  344.                 SendMessage(combo, CB_RESETCONTENT, 0, 0);
  345.  
  346.                 // add new stings according to if item is enabled or not
  347.                 if (obj->d_rendererAvailability[OgreGuiRendererType])
  348.                 {
  349.                     idx = static_cast<int>(SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(_TEXT("Ogre Engine Renderer"))));
  350.                     SendMessage(combo, CB_SETITEMDATA, idx, OgreGuiRendererType);
  351.                 }
  352.  
  353.                 if (obj->d_rendererAvailability[Direct3D81GuiRendererType])
  354.                 {
  355.                     idx = static_cast<int>(SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(_TEXT("Microsoft Direct3D 8.1 Renderer"))));
  356.                     SendMessage(combo, CB_SETITEMDATA, idx, Direct3D81GuiRendererType);
  357.                 }
  358.  
  359.                 if (obj->d_rendererAvailability[Direct3D9GuiRendererType])
  360.                 {
  361.                     idx = static_cast<int>(SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(_TEXT("Microsoft Direct3D 9 Renderer"))));
  362.                     SendMessage(combo, CB_SETITEMDATA, idx, Direct3D9GuiRendererType);
  363.                 }
  364.  
  365.                 if (obj->d_rendererAvailability[OpenGLGuiRendererType])
  366.                 {
  367.                     idx = static_cast<int>(SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(_TEXT("OpenGL Renderer"))));
  368.                     SendMessage(combo, CB_SETITEMDATA, idx, OpenGLGuiRendererType);
  369.                 }
  370.  
  371.                 if (obj->d_rendererAvailability[IrrlichtGuiRendererType])
  372.                 {
  373.                     idx = static_cast<int>(SendMessage(combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(_TEXT("Irrlicht Engine Renderer"))));
  374.                     SendMessage(combo, CB_SETITEMDATA, idx, IrrlichtGuiRendererType);
  375.                 }
  376.  
  377.             }
  378.  
  379.             return FALSE;
  380.         }
  381.  
  382.     case WM_COMMAND:
  383.         switch(LOWORD(wParam))
  384.         {
  385.         case IDOK:
  386.             EndDialog(hDlg, 1);
  387.             return TRUE;
  388.  
  389.         case IDCANCEL:
  390.             EndDialog(hDlg, 0);
  391.             return TRUE;
  392.  
  393.             // Combo-box
  394.         case 1000:
  395.             switch(HIWORD(wParam))
  396.             {
  397.             case CBN_SELENDOK:
  398.                 {
  399.                     HWND combo = reinterpret_cast<HWND>(lParam);
  400.  
  401.                     // get the 'this' ptr for the object we were created by
  402.                     Win32CEGuiRendererSelector* obj = reinterpret_cast<Win32CEGuiRendererSelector*>(GetWindowLong(hDlg, DWL_USER));
  403.  
  404.                     int idx = static_cast<int>(SendMessage(combo, CB_GETCURSEL, 0, 0));
  405.  
  406.                     if (idx != CB_ERR)
  407.                     {
  408.                         // set last selected renderer type
  409.                         obj->d_lastSelected = static_cast<CEGuiRendererType>(SendMessage(combo, CB_GETITEMDATA, idx, 0));
  410.                     }
  411.  
  412.                     return TRUE;
  413.                 }
  414.             }
  415.         }
  416.  
  417.         break;
  418.     }
  419.  
  420.     return FALSE;
  421. }
  422.  
  423.  
  424. /*************************************************************************
  425.     Take an input pointer, return closest pointer that is aligned on a
  426.     DWORD (4 byte) boundary.
  427. *************************************************************************/
  428. LPBYTE Win32CEGuiRendererSelector::alignPointer(LPBYTE buff)
  429. {
  430.     DWORD_PTR ul = reinterpret_cast<DWORD_PTR>(buff);
  431.     ul +=3;
  432.     ul >>=2;
  433.     ul <<=2;
  434.     return reinterpret_cast<LPBYTE>(ul);
  435. }
  436.  
  437.  
  438. /*************************************************************************
  439.     Converts the Ansi string in 'pAnsiIn' into wide characters and
  440.     copies the result into the WORD array at 'pWCStr'.
  441. *************************************************************************/
  442. int Win32CEGuiRendererSelector::copyAnsiToWideChar(LPBYTE outBuff, PTSTR ansiString)
  443. {
  444.     LPWSTR pWCStr = reinterpret_cast<LPWSTR>(outBuff);
  445.  
  446. #   ifdef UNICODE
  447.     return lstrlen(lstrcpy(pWCStr, ansiString)) + 1;
  448. #   else
  449.     int cchAnsi = lstrlen(ansiString);
  450.     return MultiByteToWideChar(GetACP(), MB_PRECOMPOSED, ansiString, cchAnsi, pWCStr, cchAnsi) + 1;
  451. #   endif
  452. }
  453.